<?php


class Blackbird_PollsController extends Zend_Controller_Action
{
	function resultsAction()
	{
		$slug = $this->_getParam('slug');
		$model = new Blackbird_Model_Polls();
		$poll = $model->fetchPollBySlug($slug);
		if (false === $poll)
		{
			$this->_helper->flashMessenger("No ha sido posible encontrar la encuesta '$slug'.");
			return $this->_redirect(BASE_URL, array('code' => 303));
		}
		$this->view->poll = $poll;
	}

	function voteAction()
	{
		// TODO: Replace with something more robust and per poll.
		$session = new Zend_Session_Namespace('polls/vote');
		if (isset($session->hasVoted))
			$this->_redirect(BASE_URL . '/Blackbird/polls/results/slug/' . $slug, array('code' => 303));

		$slug = $this->_getParam('slug');
		$pollOptionId = $this->getRequest()->getPost('pollOptionId');
		$model = new Blackbird_Model_Polls();
		if (!$model->votePollBySlug($slug, $pollOptionId))
		{
			$this->_helper->flashMessenger("No ha sido posible encontrar la encuesta '$slug'.");
			return $this->_redirect(BASE_URL, array('code' => 303));
		}
		$session->hasVoted = true;
		$this->_redirect(BASE_URL . '/Blackbird/polls/results/slug/' . $slug, array('code' => 303));
	}
}

